/**
 * Copyright 2013-2023 Software Radio Systems Limited
 *
 * This file is part of srsRAN.
 *
 * srsRAN is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * srsRAN is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * A copy of the GNU Affero General Public License can be found in
 * the LICENSE file in the top-level directory of this distribution
 * and at http://www.gnu.org/licenses/.
 *
 */

#include "srsran/common/bcd_helpers.h"
#include "srsran/common/common.h"
#include "srsran/common/security.h"
#include "srsran/common/test_common.h"
#include "srsran/srslog/srslog.h"

using namespace srsran;

int arrcmp(uint8_t const* const a, uint8_t const* const b, uint32_t len)
{
  uint32_t i = 0;

  for (i = 0; i < len; i++) {
    if (a[i] != b[i]) {
      return a[i] - b[i];
    }
  }
  return SRSRAN_SUCCESS;
}

int test_set_ksg()
{
  auto& logger  = srslog::fetch_basic_logger("LOG", false);
  int   err_lte = SRSRAN_ERROR;
  int   err_cmp = SRSRAN_ERROR;

  uint8_t  k_enb[] = {0xfe, 0x7d, 0xee, 0x80, 0x8d, 0x7f, 0x3b, 0x88, 0x2a, 0x08, 0x2c, 0xbd, 0xc8, 0x39, 0x0d, 0x12,
                     0x9e, 0x5d, 0x28, 0xaf, 0x0e, 0x83, 0x22, 0xeb, 0x57, 0x3a, 0xda, 0x36, 0xf2, 0x1a, 0x5a, 0x89};
  uint8_t  sk_gnb_o[32];
  uint16_t scg_counter = 0;
  err_lte              = srsran::security_generate_sk_gnb(k_enb, scg_counter, sk_gnb_o);
  TESTASSERT(err_lte == SRSRAN_SUCCESS);
  logger.info(&sk_gnb_o[0], sizeof(sk_gnb_o), "sk gnb o:");
  uint8_t sk_gnb[] = {0x45, 0xcb, 0xc3, 0xf8, 0xa8, 0x11, 0x93, 0xfd, 0x5c, 0x52, 0x29, 0x30, 0x0d, 0x59, 0xed, 0xf8,
                      0x12, 0xe9, 0x98, 0xa1, 0x15, 0xec, 0x4e, 0x0c, 0xe9, 0x03, 0xba, 0x89, 0x36, 0x7e, 0x26, 0x28};
  err_cmp          = arrcmp(sk_gnb_o, sk_gnb, sizeof(sk_gnb));
  TESTASSERT(err_cmp == 0);
  return SRSRAN_SUCCESS;
}

int test_set_nr_rrc_up()
{
  auto& logger  = srslog::fetch_basic_logger("LOG", false);
  int   err_lte = SRSRAN_ERROR;
  int   err_cmp = SRSRAN_ERROR;

  uint8_t sk_gnb[] = {0x45, 0xcb, 0xc3, 0xf8, 0xa8, 0x11, 0x93, 0xfd, 0x5c, 0x52, 0x29, 0x30, 0x0d, 0x59, 0xed, 0xf8,
                      0x12, 0xe9, 0x98, 0xa1, 0x15, 0xec, 0x4e, 0x0c, 0xe9, 0x03, 0xba, 0x89, 0x36, 0x7e, 0x26, 0x28};

  uint8_t sk_gnb_o[32];
  uint8_t k_rrc_enc_o[32];
  uint8_t k_rrc_int_o[32];

  err_lte = srsran::security_generate_k_nr_rrc(sk_gnb,
                                               srsran::CIPHERING_ALGORITHM_ID_ENUM::CIPHERING_ALGORITHM_ID_128_EEA2,
                                               srsran::INTEGRITY_ALGORITHM_ID_ENUM::INTEGRITY_ALGORITHM_ID_EIA0,
                                               k_rrc_enc_o,
                                               k_rrc_int_o);

  TESTASSERT(err_lte == SRSRAN_SUCCESS);
  logger.info(&k_rrc_enc_o[0], sizeof(k_rrc_enc_o), "RRC ENC output:");

  uint8_t k_rrc_enc[] = {0x52, 0xa9, 0x95, 0xdf, 0xf8, 0x9b, 0xc2, 0x94, 0xbd, 0x89, 0xff,
                         0xb1, 0x37, 0xa2, 0x9f, 0x24, 0x66, 0xa0, 0x9e, 0x99, 0x23, 0x86,
                         0xc8, 0xd1, 0xdf, 0x78, 0x92, 0x96, 0x4c, 0x6f, 0xb5, 0x22};

  err_cmp = arrcmp(k_rrc_enc_o, k_rrc_enc, sizeof(k_rrc_enc_o));

  TESTASSERT(err_cmp == 0);

  uint8_t k_up_enc_o[32];
  uint8_t k_up_int_o[32];

  err_lte = srsran::security_generate_k_nr_up(sk_gnb,
                                              srsran::CIPHERING_ALGORITHM_ID_ENUM::CIPHERING_ALGORITHM_ID_128_EEA2,
                                              srsran::INTEGRITY_ALGORITHM_ID_ENUM::INTEGRITY_ALGORITHM_ID_EIA0,
                                              k_up_enc_o,
                                              k_up_int_o);

  uint8_t k_up_enc[] = {0x7c, 0xe2, 0x06, 0x70, 0xbb, 0xbc, 0xc5, 0x90, 0x40, 0x87, 0xc0, 0xd4, 0x26, 0x53, 0xc5, 0x40,
                        0x15, 0x20, 0x52, 0xd3, 0xdf, 0xbc, 0x3f, 0x05, 0x86, 0x9b, 0x7f, 0x92, 0x00, 0x95, 0xbe, 0x68};

  logger.info(&k_up_enc_o[0], sizeof(k_up_enc_o), "UP ENC output:");

  err_cmp = arrcmp(k_up_enc_o, k_up_enc, sizeof(k_up_enc_o));
  TESTASSERT(err_cmp == 0);
  return SRSRAN_SUCCESS;
}

int test_generate_k_asme()
{
  auto& logger = srslog::fetch_basic_logger("LOG", false);

  uint8_t  k_asme_o[32] = {};
  uint8_t  ck[]  = {0x7b, 0xc1, 0x5d, 0x69, 0x30, 0x9c, 0xf3, 0xec, 0x5d, 0x32, 0x44, 0x04, 0xed, 0xd6, 0xf0, 0xf9};
  uint8_t  ik[]  = {0xc1, 0x5d, 0x69, 0x30, 0x9c, 0xf3, 0xec, 0x5d, 0x32, 0x44, 0x04, 0xed, 0xd6, 0xf0, 0xf9, 0x7b};
  uint8_t  ak[]  = {0x5d, 0x69, 0x30, 0x9c, 0xf3, 0xec};
  uint8_t  sqn[] = {0x00, 0x00, 0x00, 0x00, 0x12, 0xd9};
  uint16_t mcc   = 61441; // MCC 001
  uint16_t mnc   = 65281; // MNC 01

  uint8_t ak_xor_sqn[6];
  for (uint32_t i = 0; i < 6; i++) {
    ak_xor_sqn[i] = sqn[i] ^ ak[i];
  }
  // Generate K_asme
  security_generate_k_asme(ck, ik, ak_xor_sqn, mcc, mnc, k_asme_o);

  uint8_t k_asme[] = {0xd5, 0xef, 0x4d, 0x8f, 0x33, 0x26, 0x69, 0x02, 0x29, 0x5d, 0x42, 0xf3, 0x22, 0xa2, 0xf2, 0xcf,
                      0x11, 0xfb, 0x2c, 0xcc, 0x12, 0x4c, 0x09, 0xb4, 0xd8, 0x8d, 0x36, 0x15, 0x97, 0x03, 0x79, 0x90};

  logger.info(k_asme_o, 32, "K_ASME output:");
  TESTASSERT(arrcmp(k_asme, k_asme_o, sizeof(k_asme_o)) == 0);

  return SRSRAN_SUCCESS;
}

int test_generate_k_nas()
{
  auto&   logger = srslog::fetch_basic_logger("LOG", false);
  uint8_t k_nas_enc_o[32];
  uint8_t k_nas_int_o[32];

  uint8_t k_asme[] = {0xd5, 0xef, 0x4d, 0x8f, 0x33, 0x26, 0x69, 0x02, 0x29, 0x5d, 0x42, 0xf3, 0x22, 0xa2, 0xf2, 0xcf,
                      0x11, 0xfb, 0x2c, 0xcc, 0x12, 0x4c, 0x09, 0xb4, 0xd8, 0x8d, 0x36, 0x15, 0x97, 0x03, 0x79, 0x90};

  TESTASSERT(srsran::security_generate_k_nas(k_asme,
                                             srsran::CIPHERING_ALGORITHM_ID_ENUM::CIPHERING_ALGORITHM_ID_EEA0,
                                             srsran::INTEGRITY_ALGORITHM_ID_ENUM::INTEGRITY_ALGORITHM_ID_128_EIA1,
                                             k_nas_enc_o,
                                             k_nas_int_o) == SRSRAN_SUCCESS);

  uint8_t k_nas_enc[] = {0xcf, 0x7a, 0x27, 0xbe, 0xab, 0x1e, 0x78, 0xbf, 0x5b, 0xb1, 0xf9,
                         0x5b, 0x05, 0x82, 0x9a, 0x72, 0xe4, 0xcf, 0x7f, 0xec, 0xba, 0xd4,
                         0xb8, 0xc3, 0x7d, 0xb4, 0xa1, 0x90, 0xe1, 0x19, 0x22, 0x5c};
  logger.info(k_nas_enc_o, 32, "K NAS ENC output:");
  TESTASSERT(arrcmp(k_nas_enc, k_nas_enc_o, sizeof(k_nas_enc_o)) == 0);

  uint8_t k_nas_int[] = {0xb8, 0x55, 0x87, 0x1d, 0x17, 0xd4, 0xa7, 0x17, 0xe9, 0x31, 0xd9,
                         0xa6, 0x06, 0x9c, 0x6e, 0x54, 0xb7, 0x94, 0x08, 0xe2, 0x43, 0xdd,
                         0x36, 0x7d, 0xc8, 0xc6, 0x39, 0x22, 0x95, 0xb3, 0xb4, 0x51};

  logger.info(k_nas_int_o, 32, "K NAS INT output:");
  TESTASSERT(arrcmp(k_nas_int, k_nas_int_o, sizeof(k_nas_int_o)) == 0);

  return SRSRAN_SUCCESS;
}

int test_generate_k_gnb()
{
  auto& logger = srslog::fetch_basic_logger("LOG", false);

  as_key_t k_gnb_o;

  as_key_t k_gnb = {0x49, 0x3a, 0x16, 0xc5, 0x8b, 0x77, 0xb6, 0x27, 0xfa, 0x3f, 0x1a, 0xc6, 0x34, 0x4c, 0x18, 0x30,
                    0x39, 0xf0, 0x1b, 0xa0, 0xcb, 0x76, 0x36, 0xbb, 0xcc, 0xc4, 0x36, 0x5b, 0x02, 0x3b, 0xd5, 0x62};

  as_key_t k_amf = {0xd6, 0x55, 0xf1, 0x61, 0x42, 0x03, 0x5d, 0x4d, 0x72, 0xca, 0x39, 0x58, 0x3d, 0x22, 0x8d, 0x2d,
                    0xd2, 0xec, 0x0c, 0xa7, 0x92, 0x9a, 0xd0, 0x07, 0xf5, 0x3b, 0x38, 0x2d, 0x05, 0x54, 0x44, 0x05};

  uint32_t nas_ul_count = 0;

  TESTASSERT(srsran::security_generate_k_gnb(k_amf, nas_ul_count, k_gnb_o) == SRSRAN_SUCCESS);
  TESTASSERT(k_gnb_o == k_gnb);

  return SRSRAN_SUCCESS;
}

int test_generate_k_enb()
{
  auto&   logger = srslog::fetch_basic_logger("LOG", false);
  uint8_t k_enb_o[32];

  uint8_t  k_asme[] = {0x61, 0x44, 0xc6, 0x81, 0xd1, 0xbe, 0xa9, 0xda, 0xe1, 0xb8, 0xcf, 0x6c, 0xd1, 0x0a, 0x68, 0x63,
                      0x41, 0xdb, 0x80, 0x46, 0xa1, 0xe7, 0xa9, 0xab, 0x4d, 0x1e, 0xa0, 0xe3, 0x3c, 0x99, 0x4a, 0xc0};
  uint32_t nas_ul_count = 0;

  TESTASSERT(srsran::security_generate_k_enb(k_asme, nas_ul_count, k_enb_o) == SRSRAN_SUCCESS);

  uint8_t k_enb[] = {0xc4, 0xc7, 0xbc, 0x79, 0x8a, 0xb9, 0x4e, 0x3d, 0x35, 0x4c, 0xd6, 0x60, 0x8e, 0x79, 0xaa, 0x92,
                     0xf5, 0x56, 0x9d, 0xf4, 0x65, 0x19, 0x50, 0x78, 0x50, 0x05, 0x1e, 0x36, 0xf0, 0x18, 0xca, 0x5f};

  logger.info(k_enb, 32, "K ENB output:");
  TESTASSERT(arrcmp(k_enb, k_enb_o, sizeof(k_enb_o)) == 0);

  return SRSRAN_SUCCESS;
}

int test_generate_rrc_keys()
{
  auto&   logger = srslog::fetch_basic_logger("LOG", false);
  uint8_t k_rrc_enc_o[32];
  uint8_t k_rrc_int_o[32];

  uint8_t k_enb[] = {0xc4, 0xc7, 0xbc, 0x79, 0x8a, 0xb9, 0x4e, 0x3d, 0x35, 0x4c, 0xd6, 0x60, 0x8e, 0x79, 0xaa, 0x92,
                     0xf5, 0x56, 0x9d, 0xf4, 0x65, 0x19, 0x50, 0x78, 0x50, 0x05, 0x1e, 0x36, 0xf0, 0x18, 0xca, 0x5f};

  TESTASSERT(srsran::security_generate_k_rrc(k_enb,
                                             srsran::CIPHERING_ALGORITHM_ID_ENUM::CIPHERING_ALGORITHM_ID_EEA0,
                                             srsran::INTEGRITY_ALGORITHM_ID_ENUM::INTEGRITY_ALGORITHM_ID_128_EIA2,
                                             k_rrc_enc_o,
                                             k_rrc_int_o) == SRSRAN_SUCCESS);

  uint8_t k_rrc_enc[] = {0x23, 0xaf, 0xdd, 0x7b, 0x2e, 0x4a, 0x5a, 0x99, 0xc7, 0x78, 0x82,
                         0x78, 0x59, 0xcf, 0x45, 0x14, 0x86, 0xa2, 0x75, 0x78, 0x8b, 0x6f,
                         0x36, 0xa5, 0xb9, 0xb8, 0x10, 0xf5, 0xd4, 0x72, 0xa2, 0x4b};

  logger.info(k_rrc_enc_o, 32, "K RRC ENC output:");
  TESTASSERT(arrcmp(k_rrc_enc, k_rrc_enc_o, sizeof(k_rrc_enc_o)) == 0);

  uint8_t k_rrc_int[] = {0x42, 0xf4, 0xf8, 0xc5, 0x85, 0xae, 0x89, 0x05, 0xa5, 0x0d, 0x0f,
                         0x32, 0xa9, 0x79, 0xd8, 0xb6, 0xfa, 0x1d, 0x6d, 0x19, 0x40, 0xf1,
                         0xd8, 0x79, 0xcf, 0x01, 0x89, 0x34, 0x2a, 0x8d, 0x73, 0xc2};

  logger.info(k_rrc_int_o, 32, "K RRC INT output:");
  TESTASSERT(arrcmp(k_rrc_int, k_rrc_int_o, sizeof(k_rrc_int_o)) == 0);

  return SRSRAN_SUCCESS;
}

int test_generate_up_keys()
{
  auto&   logger = srslog::fetch_basic_logger("LOG", false);
  uint8_t k_up_enc_o[32];
  uint8_t k_up_int_o[32];

  uint8_t k_enb[] = {0xc4, 0xc7, 0xbc, 0x79, 0x8a, 0xb9, 0x4e, 0x3d, 0x35, 0x4c, 0xd6, 0x60, 0x8e, 0x79, 0xaa, 0x92,
                     0xf5, 0x56, 0x9d, 0xf4, 0x65, 0x19, 0x50, 0x78, 0x50, 0x05, 0x1e, 0x36, 0xf0, 0x18, 0xca, 0x5f};

  TESTASSERT(srsran::security_generate_k_up(k_enb,
                                            srsran::CIPHERING_ALGORITHM_ID_ENUM::CIPHERING_ALGORITHM_ID_EEA0,
                                            srsran::INTEGRITY_ALGORITHM_ID_ENUM::INTEGRITY_ALGORITHM_ID_128_EIA2,
                                            k_up_enc_o,
                                            k_up_int_o) == SRSRAN_SUCCESS);

  uint8_t k_up_enc[] = {0x22, 0xbf, 0xb5, 0x87, 0x61, 0xca, 0x1d, 0xd3, 0xb2, 0x0a, 0x28, 0x1c, 0x7e, 0xab, 0x0c, 0x0b,
                        0x9c, 0x3c, 0x92, 0xe1, 0xdd, 0xc0, 0xc8, 0xc5, 0x70, 0x6c, 0xbb, 0x8f, 0x95, 0x5e, 0x82, 0x63};

  logger.info(k_up_enc_o, 32, "K UP ENC output:");
  TESTASSERT(arrcmp(k_up_enc, k_up_enc_o, sizeof(k_up_enc_o)) == 0);

  uint8_t k_up_int[] = {0x82, 0x0d, 0xcd, 0xf3, 0xb9, 0xc9, 0x4b, 0x32, 0xf8, 0x41, 0xc2, 0x5c, 0xc8, 0x78, 0xaa, 0x07,
                        0x77, 0x16, 0xc7, 0x83, 0xa5, 0x3f, 0xd3, 0xee, 0x58, 0x2f, 0xc5, 0x69, 0xe9, 0xc3, 0x3c, 0xa7};

  logger.info(k_up_int_o, 32, "K UP INT output:");
  TESTASSERT(arrcmp(k_up_int, k_up_int_o, sizeof(k_up_int_o)) == 0);

  return SRSRAN_SUCCESS;
}

int test_generate_k_enb_star()
{
  auto&   logger = srslog::fetch_basic_logger("LOG", false);
  uint8_t k_enb_star_o[32];

  uint8_t k_enb[] = {0xc4, 0xc7, 0xbc, 0x79, 0x8a, 0xb9, 0x4e, 0x3d, 0x35, 0x4c, 0xd6, 0x60, 0x8e, 0x79, 0xaa, 0x92,
                     0xf5, 0x56, 0x9d, 0xf4, 0x65, 0x19, 0x50, 0x78, 0x50, 0x05, 0x1e, 0x36, 0xf0, 0x18, 0xca, 0x5f};

  TESTASSERT(srsran::security_generate_k_enb_star(k_enb, 32, 561, k_enb_star_o) == SRSRAN_SUCCESS);

  uint8_t k_enb_star[] = {0x6f, 0x04, 0x54, 0x57, 0x7f, 0x55, 0x5c, 0xa7, 0x6f, 0x44, 0x9c,
                          0x2a, 0xc1, 0xda, 0x0f, 0x4f, 0xb7, 0xde, 0x4b, 0x94, 0x40, 0x11,
                          0x0f, 0xfb, 0xd1, 0x2f, 0x3a, 0x5d, 0x4d, 0xe1, 0x73, 0x50};

  logger.info(k_enb_star_o, 32, "K ENB STAR:");
  TESTASSERT(arrcmp(k_enb_star_o, k_enb_star, sizeof(k_enb_star)) == 0);

  return SRSRAN_SUCCESS;
}

int test_generate_k_nh()
{
  auto&   logger = srslog::fetch_basic_logger("LOG", false);
  uint8_t nh_o[32];

  uint8_t k_asme[] = {0xd5, 0xef, 0x4d, 0x8f, 0x33, 0x26, 0x69, 0x02, 0x29, 0x5d, 0x42, 0xf3, 0x22, 0xa2, 0xf2, 0xcf,
                      0x11, 0xfb, 0x2c, 0xcc, 0x12, 0x4c, 0x09, 0xb4, 0xd8, 0x8d, 0x36, 0x15, 0x97, 0x03, 0x79, 0x90};

  uint8_t sync[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12};

  TESTASSERT(srsran::security_generate_nh(k_asme, sync, nh_o) == SRSRAN_SUCCESS);

  uint8_t nh[] = {0xc8, 0x55, 0x86, 0x64, 0x13, 0x07, 0x1b, 0x74, 0xab, 0xa0, 0x1c, 0x13, 0x2b, 0x94, 0x3e, 0x76,
                  0x98, 0xaa, 0x5c, 0x11, 0x76, 0x0d, 0x9b, 0xb1, 0x16, 0x2a, 0xb3, 0x13, 0xb1, 0x1c, 0x60, 0x51};

  logger.info(nh_o, 32, "NH:");
  TESTASSERT(arrcmp(nh_o, nh, sizeof(nh)) == 0);

  return SRSRAN_SUCCESS;
}

int test_generate_res_star()
{
  auto&   logger = srslog::fetch_basic_logger("LOG", false);
  uint8_t res_star_o[16];

  uint8_t     ck[]   = {0x3c, 0xba, 0x90, 0x25, 0x75, 0xed, 0x80, 0xcb, 0xfa, 0x36, 0x25, 0xaf, 0xf0, 0x9d, 0xaf, 0xfc};
  uint8_t     ik[]   = {0xba, 0x90, 0x25, 0x75, 0xed, 0x80, 0xcb, 0xfa, 0x36, 0x25, 0xaf, 0xf0, 0x9d, 0xaf, 0xfc, 0x3c};
  uint8_t     rand[] = {0xfc, 0x2d, 0x98, 0xa3, 0x61, 0x20, 0x8b, 0xf7, 0x43, 0x63, 0x9c, 0x9e, 0x63, 0x2d, 0x73, 0x50};
  uint8_t     res[]  = {0xfc, 0x3c, 0xba, 0x90, 0x25, 0x75, 0xed, 0x80};
  std::string ssn    = "5G:mnc001.mcc001.3gppnetwork.org";

  TESTASSERT(srsran::security_generate_res_star(ck, ik, ssn.c_str(), rand, res, sizeof(res), res_star_o) ==
             SRSRAN_SUCCESS);

  uint8_t res_star[] = {0xb0, 0xe3, 0x5b, 0x23, 0xdb, 0xd7, 0xa1, 0x8c, 0x84, 0x8b, 0xfa, 0xd9, 0x11, 0x35, 0xe3, 0xfd};

  logger.info(res_star_o, 16, "RES STAR:");
  TESTASSERT(arrcmp(res_star_o, res_star, sizeof(res_star)) == 0);

  return SRSRAN_SUCCESS;
}

int test_generate_k_ausf()
{
  auto&   logger = srslog::fetch_basic_logger("LOG", false);
  uint8_t k_ausf_o[32];

  uint8_t     ck[] = {0x56, 0x1e, 0x05, 0xef, 0xbd, 0xf2, 0xef, 0xeb, 0x2d, 0x55, 0x8f, 0x04, 0x1c, 0x53, 0xc4, 0x45};
  uint8_t     ik[] = {0x01, 0xe0, 0xf2, 0xf5, 0x53, 0x54, 0x31, 0x31, 0x2d, 0x57, 0x27, 0x98, 0x14, 0xcf, 0xcd, 0x89};
  uint8_t     ak_xor_sqn[] = {0x30, 0x5e, 0xb0, 0x6b, 0x73, 0x07};
  std::string ssn          = "5G:mnc070.mcc901.3gppnetwork.org";

  TESTASSERT(srsran::security_generate_k_ausf(ck, ik, ak_xor_sqn, ssn.c_str(), k_ausf_o) == SRSRAN_SUCCESS);

  uint8_t k_ausf[] = {0xa2, 0xce, 0xb2, 0x0f, 0x79, 0x28, 0xbf, 0x15, 0x4d, 0x4b, 0x54, 0x8a, 0xee, 0x6d, 0x10, 0xa9,
                      0x76, 0x01, 0x84, 0x7f, 0xd7, 0x2d, 0x2b, 0xc9, 0x01, 0x98, 0x2c, 0x08, 0x6e, 0xa0, 0xf3, 0x46};
  //  TODO check the out put with k AMF
  logger.info(k_ausf_o, 32, "K AUSF:");
  TESTASSERT(arrcmp(k_ausf_o, k_ausf, sizeof(k_ausf)) == 0);

  return SRSRAN_SUCCESS;
}

int test_generate_k_seaf()
{
  auto&   logger = srslog::fetch_basic_logger("LOG", false);
  uint8_t k_seaf_o[32];

  uint8_t k_ausf[] = {0xd9, 0xc8, 0xff, 0x91, 0xb6, 0x9e, 0x25, 0x0e, 0x25, 0x6e, 0x92, 0x46, 0x6a, 0xcf, 0x80, 0xa1,
                      0xd8, 0x2b, 0x65, 0xf0, 0x94, 0xb7, 0xc0, 0x71, 0x19, 0x9c, 0x03, 0x12, 0xe0, 0x67, 0xff, 0x3b};
  std::string ssn  = "5G:mnc070.mcc901.3gppnetwork.org";

  TESTASSERT(srsran::security_generate_k_seaf(k_ausf, ssn.c_str(), k_seaf_o) == SRSRAN_SUCCESS);

  uint8_t k_seaf[] = {0x6c, 0x50, 0xbf, 0xa5, 0xf3, 0x2a, 0x89, 0xad, 0xe1, 0xee, 0x6c, 0x70, 0x7d, 0xe6, 0xdc, 0xfe,
                      0xa0, 0x79, 0x0a, 0xfb, 0x6d, 0x14, 0xf9, 0xe5, 0x59, 0x43, 0xae, 0xda, 0x58, 0x33, 0x45, 0x48};

  logger.info(k_seaf_o, 32, "K SEAF:");
  TESTASSERT(arrcmp(k_seaf_o, k_seaf, sizeof(k_seaf)) == 0);

  return SRSRAN_SUCCESS;
}

int test_generate_k_amf()
{
  auto&   logger = srslog::fetch_basic_logger("LOG", false);
  uint8_t k_amf_o[32];

  uint8_t k_seaf[] = {0x6c, 0x50, 0xbf, 0xa5, 0xf3, 0x2a, 0x89, 0xad, 0xe1, 0xee, 0x6c, 0x70, 0x7d, 0xe6, 0xdc, 0xfe,
                      0xa0, 0x79, 0x0a, 0xfb, 0x6d, 0x14, 0xf9, 0xe5, 0x59, 0x43, 0xae, 0xda, 0x58, 0x33, 0x45, 0x48};

  uint8_t     abba[] = {0x00, 0x00};
  std::string supi   = "901700000021309";

  TESTASSERT(srsran::security_generate_k_amf(k_seaf, supi.c_str(), abba, sizeof(abba), k_amf_o) == SRSRAN_SUCCESS);

  uint8_t k_amf[] = {0x46, 0x23, 0x7e, 0xf0, 0x51, 0xd6, 0xa1, 0x6a, 0x39, 0x27, 0x52, 0x5f, 0xa6, 0x5c, 0xc3, 0x95,
                     0x44, 0xcd, 0xfd, 0x79, 0x90, 0xb9, 0x46, 0xad, 0x79, 0x65, 0x30, 0xe6, 0xd7, 0x87, 0xa0, 0xda};

  logger.info(k_amf_o, 32, "K AMF:");
  TESTASSERT(arrcmp(k_amf_o, k_amf, sizeof(k_amf)) == 0);

  return SRSRAN_SUCCESS;
}

int test_generate_k_amf_2()
{
  auto&   logger = srslog::fetch_basic_logger("LOG", false);
  uint8_t k_amf_o[32];

  uint8_t k_seaf[] = {0x30, 0x2a, 0xb7, 0xc9, 0x4f, 0xae, 0x97, 0xe0, 0x43, 0xb3, 0x70, 0xd3, 0xeb, 0x1f, 0x54, 0x68,
                      0xf6, 0xa1, 0xd4, 0x55, 0xd6, 0x56, 0xa4, 0xca, 0xb3, 0x51, 0xea, 0xcf, 0xb9, 0xf7, 0xe1, 0x36};

  uint8_t     abba[] = {0x00, 0x00};
  std::string supi   = "001010123456780";

  TESTASSERT(srsran::security_generate_k_amf(k_seaf, supi.c_str(), abba, sizeof(abba), k_amf_o) == SRSRAN_SUCCESS);

  uint8_t k_amf[] = {0x8f, 0x2a, 0x5d, 0x6c, 0x12, 0x60, 0x8d, 0x8a, 0x0a, 0x8d, 0x33, 0x0a, 0x71, 0x9d, 0xee, 0x07,
                     0x22, 0x6b, 0x47, 0x41, 0x7b, 0x66, 0x1a, 0xf5, 0x84, 0x90, 0xea, 0xb0, 0x7a, 0xa4, 0x37, 0xa4};

  logger.info(k_amf_o, 32, "K AMF:");
  TESTASSERT(arrcmp(k_amf_o, k_amf, sizeof(k_amf)) == 0);

  return SRSRAN_SUCCESS;
}

int test_generate_nas_5g()
{
  auto&   logger = srslog::fetch_basic_logger("LOG", false);
  uint8_t k_nas_int_o[32];
  uint8_t k_nas_enc_o[32];

  uint8_t k_amf[] = {0x7d, 0x86, 0x18, 0x52, 0xb4, 0x2a, 0x86, 0xb5, 0x96, 0xfe, 0x22, 0xc8, 0xf5, 0x0b, 0x9b, 0x89,
                     0x5a, 0x1e, 0x21, 0x71, 0x6b, 0x61, 0xb8, 0xd1, 0x22, 0x78, 0x5e, 0x25, 0xba, 0xfc, 0x0d, 0x07};

  TESTASSERT(srsran::security_generate_k_nas_5g(k_amf,
                                                srsran::CIPHERING_ALGORITHM_ID_ENUM::CIPHERING_ALGORITHM_ID_EEA0,
                                                srsran::INTEGRITY_ALGORITHM_ID_ENUM::INTEGRITY_ALGORITHM_ID_128_EIA2,
                                                k_nas_enc_o,
                                                k_nas_int_o) == SRSRAN_SUCCESS);

  uint8_t k_nas_enc[] = {0x83, 0x85, 0x6a, 0x01, 0x81, 0x59, 0x55, 0x21, 0xc9, 0xd3, 0x2a,
                         0x19, 0x3f, 0x59, 0xc8, 0xa8, 0xfc, 0x7e, 0x99, 0xb9, 0x8b, 0x8c,
                         0x04, 0x20, 0x1f, 0x4f, 0x9d, 0x81, 0x43, 0x3b, 0x6b, 0x38};
  uint8_t k_nas_int[] = {0x25, 0xc5, 0x8e, 0x27, 0xe7, 0xd2, 0xc4, 0x95, 0xf3, 0xd7, 0x35,
                         0x29, 0x3a, 0x82, 0xbf, 0xfa, 0xa5, 0x3e, 0xe8, 0x84, 0xb9, 0x8d,
                         0x36, 0x6b, 0x02, 0x89, 0x23, 0x1e, 0xc4, 0x19, 0x1e, 0xd3};

  logger.info(k_nas_enc_o, 32, "k_nas_enc_o:");
  TESTASSERT(arrcmp(k_nas_enc_o, k_nas_enc, sizeof(k_nas_enc)) == 0);
  logger.info(k_nas_int_o, 32, "k_nas_int_o:");
  TESTASSERT(arrcmp(k_nas_int_o, k_nas_int, sizeof(k_nas_int)) == 0);
  return SRSRAN_SUCCESS;
}

int main(int argc, char** argv)
{
  auto& logger = srslog::fetch_basic_logger("LOG", false);
  logger.set_level(srslog::basic_levels::debug);
  logger.set_hex_dump_max_size(256);

  srslog::init();

  TESTASSERT(test_generate_k_asme() == SRSRAN_SUCCESS);
  TESTASSERT(test_generate_k_nas() == SRSRAN_SUCCESS);
  TESTASSERT(test_generate_k_enb() == SRSRAN_SUCCESS);
  TESTASSERT(test_generate_rrc_keys() == SRSRAN_SUCCESS);
  TESTASSERT(test_generate_up_keys() == SRSRAN_SUCCESS);
  TESTASSERT(test_generate_k_enb_star() == SRSRAN_SUCCESS);
  TESTASSERT(test_generate_k_nh() == SRSRAN_SUCCESS);
  TESTASSERT(test_generate_k_gnb() == SRSRAN_SUCCESS);
  TESTASSERT(test_generate_res_star() == SRSRAN_SUCCESS);
  TESTASSERT(test_generate_k_ausf() == SRSRAN_SUCCESS);
  TESTASSERT(test_generate_k_seaf() == SRSRAN_SUCCESS);
  TESTASSERT(test_generate_k_amf() == SRSRAN_SUCCESS);
  TESTASSERT(test_generate_k_amf_2() == SRSRAN_SUCCESS);
  TESTASSERT(test_generate_nas_5g() == SRSRAN_SUCCESS);
  TESTASSERT(test_set_ksg() == SRSRAN_SUCCESS);
  TESTASSERT(test_set_nr_rrc_up() == SRSRAN_SUCCESS);

  return SRSRAN_SUCCESS;
}
